package com.education.expenses.eao;

import com.education.expenses.entity.Report;
import com.education.expenses.entity.ReportStatus;
import com.education.expenses.entity.User;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * User: Егор
 * Date: 15.08.12
 * Time: 14:04
 */
@Repository
public class ReportEAOImpl extends HibernateEAO<Report> implements ReportEAO{

    @Autowired
    public ReportEAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    public List<Report> getWeekReportsByStatus(Integer userId,Date date,String status){

        Criteria criteriaReport = super.createCriteria();

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

        Criteria alias = criteriaReport.createAlias("user","u");

        List reports = alias
              .add(Restrictions.eq("u.id",new Long(userId)))
              .add(Restrictions.eq("reportStatus",Enum.valueOf(ReportStatus.class,status)))
              .add(Restrictions.eq("dateReport",dateFormat.format(date)))
              .list();

        return reports;
    }
}
